package com.zzuli.practice.controller;

import com.zzuli.practice.entity.LikeResult;
import com.zzuli.practice.entity.Result;
import com.zzuli.practice.service.impl.LikeService;
import com.zzuli.practice.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/like")
public class LikeController implements Constant {

    @Autowired
    private LikeService likeService;

    @RequestMapping(value = "/likecount/{id}", method = RequestMethod.POST)
    public Result findLikeCount(@PathVariable("id") Integer id, String username){
        Result result = new Result();

        // 查询点赞数量
        long postLikeCount = likeService.findPostLikeCount(id);

        // 查询当前用户是否已经点赞
        boolean isMember = likeService.likeStatus(username, id);

        LikeResult likeResult = new LikeResult();

        likeResult.setLikeCount(postLikeCount);
        likeResult.setLikeStatus(isMember);

        result.setStatusCode(SUCCESS);
        result.setMessage("查询成功");
        result.setData(likeResult);
        return result;
    }

    @RequestMapping(value = "/support", method = RequestMethod.POST)
    public Result support(Integer id, String username){
        Result result = new Result();

        // 点赞，取消点赞
        likeService.like(username, id);

        result.setStatusCode(SUCCESS);
        result.setMessage("操作成功");
        return result;
    }

}
